﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Windows.Media.Imaging;
using System.IO;
using System.IO.IsolatedStorage;

namespace myChannel9
{
    public static class Helper
    {
        public static bool readXml()
        {
            bool result = false;

            try
            {
                //<Channel9>
                //<ID>4911</ID>
                //<Url>/Blogs/TheChannel9Team/The-9-Guys-Who-We-Are</Url>
                //<Heading>The 9 Guys - Who We Are</Heading>
                //<Type>TheChannel9Team</Type>
                //<Posted>Mar 15, 2004 at 12:06 PM</Posted>
                //<PostedDetails>15.03.2004</PostedDetails>
                //<PostedDate>20040315</PostedDate>
                //<Image_small>http://ecn.channel9.msdn.com/o9/previewImages/100/1_100x75.jpg</Image_small>
                //<Image_large>http://ecn.channel9.msdn.com/o9/previewImages/100/1_100x75.jpg</Image_large>
                //<Lenght>15 minutes, 13 seconds</Lenght>
                //<LenghtDetails>15.13</LenghtDetails>
                //<Video>http://mschnlnine.vo.llnwd.net/d1/ch9/4/7/Channel9_Producers_2004_Introduction_To_Who_We_Are.wmv</Video>
                //<Description></Description>
                //<Tags>Channel9 Team; </Tags>
                //</Channel9>

                XDocument myXml = XDocument.Load("Resources/Channel9.xml");

                List<VideoObject> channel9List = (from channel9 in myXml.Descendants("Channel9")
                                select new VideoObject()
                                {
                                    Url = channel9.Element("Url").Value,
                                    Heading = channel9.Element("Heading").Value,
                                    Type = channel9.Element("Type").Value,
                                    Posted = channel9.Element("Posted").Value,
                                    PostedDetails = channel9.Element("PostedDetails").Value,
                                    PostedDate = channel9.Element("PostedDate").Value,
                                    Image = channel9.Element("Image_small").Value,
                                    Image_large = channel9.Element("Image_large").Value,
                                    Lenght = channel9.Element("Lenght").Value,
                                    LenghtDetails = channel9.Element("LenghtDetails").Value,
                                    Video = channel9.Element("Video").Value,
                                    Description = removeHtml(channel9.Element("Description").Value),
                                    Tags = channel9.Element("Tags").Value
                                }
                                ).ToList();

                if (IsolatedStorageSettings.ApplicationSettings.Contains("channel9List"))
                    IsolatedStorageSettings.ApplicationSettings["channel9List"] = channel9List;
                else
                    IsolatedStorageSettings.ApplicationSettings.Add("channel9List", channel9List);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            return result;
        }

        public static void getAllTags()
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("channel9List"))
            {
                List<string> stringList = new List<string>();
                List<VideoObject> channel9List = IsolatedStorageSettings.ApplicationSettings["channel9List"] as List<VideoObject>;

                if (channel9List != null)
                {
                    foreach (VideoObject video in channel9List)
                    {
                        string[] words = video.Tags.Split(';');

                        foreach (string tag in words)
                        {
                            if (tag != "" && tag != " ")
                            {
                                string temp = tag.Replace(" ", "");
                                stringList.Add(temp);
                            }
                        }
                    }

                    var tempList =
                    from tag in stringList
                    orderby tag
                    group tag by tag into g
                    select new { g.Key, Count = g.Count() };

                    List<TagObject> tagsList = new List<TagObject>();
                    foreach (var temp in tempList)
                    {
                        tagsList.Add(new TagObject { Tag = temp.Key, Count = temp.Count, BoxSize = 0 });
                    }

                    List<TagObject> tagsList_new = new List<TagObject>();
                    string allWords = "a;b;c;d;e;f;;g;h;i;j;k;l;m;n;o;p;q;r;s;t;u;v;w;q;y;z";
                    string[] words2 = allWords.Split(';');

                    foreach (string word2 in words2)
                    {
                        var tempList2 =
                            from tag in tagsList
                            orderby tag.Tag
                            where tag.Tag.Substring(0, 1).ToLower().Equals(word2)
                            select tag;

                        tagsList_new.Add(new TagObject { BoxSize = 80, BoxChar = word2 });
                        tagsList_new.AddRange(tempList2.ToList());
                    }

                    var tempList3 =
                           from tag in tagsList
                           orderby tag.Tag
                           where tag.Tag.Substring(0, 1).ToLower().Equals(".") ||
                                 tag.Tag.Substring(0, 1).ToLower().Equals("1") ||
                                 tag.Tag.Substring(0, 1).ToLower().Equals("2") ||
                                 tag.Tag.Substring(0, 1).ToLower().Equals("3") ||
                                 tag.Tag.Substring(0, 1).ToLower().Equals("4") ||
                                 tag.Tag.Substring(0, 1).ToLower().Equals("5") ||
                                 tag.Tag.Substring(0, 1).ToLower().Equals("6") ||
                                 tag.Tag.Substring(0, 1).ToLower().Equals("7") ||
                                 tag.Tag.Substring(0, 1).ToLower().Equals("8") ||
                                 tag.Tag.Substring(0, 1).ToLower().Equals("9")
                           select tag;

                    tagsList_new.Add(new TagObject { BoxSize = 80, BoxChar = "" });
                    tagsList_new.AddRange(tempList3.ToList());


                    if (IsolatedStorageSettings.ApplicationSettings.Contains("tagsList"))
                        IsolatedStorageSettings.ApplicationSettings["tagsList"] = tagsList_new;
                    else
                        IsolatedStorageSettings.ApplicationSettings.Add("tagsList", tagsList_new);
                }
            }
        }

        public static bool isUnique(string url, List<VideoObject> channel9List)
        {
            bool result = true;

            if (channel9List != null)
            {
                var tempList =
                from channel9 in channel9List
                orderby channel9.PostedDate descending
                where channel9.Url.ToLower().Contains(url.ToLower())
                select channel9;

                List<VideoObject> list = tempList.ToList();

                if (list.Count() > 0)
                {
                    result = false;
                }
            }

            return result;
        }

        public static List<VideoObject> getOriginal(string url, List<VideoObject> channel9List)
        {
            List<VideoObject> list = new List<VideoObject>();

            if (channel9List != null)
            {
                var tempList =
                from channel9 in channel9List
                orderby channel9.PostedDate descending
                where channel9.Url.ToLower().Contains(url.ToLower())
                select channel9;

                list = tempList.ToList();
            }

            return list;
        }

        public static string getPostedDetails(string posted)
        {
            string temp = "", yr = "", mo = "", da = "";
            //Fri, 27 Aug 2010 20:00:00 GMT
            try
            {
                temp = posted.Substring(0, posted.LastIndexOf(":"));
                temp = temp.Substring(temp.IndexOf(",") + 1);

                da = temp.Substring(0, 3);
                mo = temp.Substring(3, 4);
                yr = temp.Substring(7, 5);

                mo = mo.Replace(" ", "");
                switch (mo)
                {
                    case "Jan":
                        mo = "01";
                        break;

                    case "Feb":
                        mo = "02";
                        break;

                    case "Mar":
                        mo = "03";
                        break;

                    case "Apr":
                        mo = "04";
                        break;

                    case "May":
                        mo = "05";
                        break;

                    case "Jun":
                        mo = "06";
                        break;

                    case "Jul":
                        mo = "07";
                        break;

                    case "Aug":
                        mo = "08";
                        break;

                    case "Sep":
                        mo = "09";
                        break;

                    case "Oct":
                        mo = "10";
                        break;

                    case "Nov":
                        mo = "11";
                        break;

                    case "Dec":
                        mo = "12";
                        break;
                }

                temp = da + "." + mo + "." + yr;
                temp = temp.Replace(" ", "");
            }
            catch { }

            return temp;
        }

        public static string getPostedDate(string posted)
        {
            string temp = "", yr = "", mo = "", da = "";
            //Fri, 27 Aug 2010 20:00:00 GMT
            try
            {
                temp = posted.Substring(0, posted.LastIndexOf(":"));
                temp = temp.Substring(temp.IndexOf(",") + 1);

                da = temp.Substring(0, 3);
                mo = temp.Substring(3, 4);
                yr = temp.Substring(7, 5);

                mo = mo.Replace(" ", "");
                switch (mo)
                {
                    case "Jan":
                        mo = "01";
                        break;

                    case "Feb":
                        mo = "02";
                        break;

                    case "Mar":
                        mo = "03";
                        break;

                    case "Apr":
                        mo = "04";
                        break;

                    case "May":
                        mo = "05";
                        break;

                    case "Jun":
                        mo = "06";
                        break;

                    case "Jul":
                        mo = "07";
                        break;

                    case "Aug":
                        mo = "08";
                        break;

                    case "Sep ":
                        mo = "09";
                        break;

                    case "Oct":
                        mo = "10";
                        break;

                    case "Nov":
                        mo = "11";
                        break;

                    case "Dec":
                        mo = "12";
                        break;
                }

                temp = yr + mo + da;
                temp = temp.Replace(" ","");
            }
            catch { }

            return temp;
        }

        public static string removeHtml(string temp)
        {
            try
            {
                temp = temp.Replace("'", "`");
                temp = temp.Replace("â€“", "–");
                temp = temp.Replace("â€™", "`");
                temp = temp.Replace("â€™", "`");
                temp = temp.Replace("</a>", "");
                temp = temp.Replace("<strong>", "");
                temp = temp.Replace("</strong>", "");
                temp = temp.Replace("<p>", "");
                temp = temp.Replace("</p>", "");
                temp = temp.Replace("\r", "");
                temp = temp.Replace("\n", "");
                temp = temp.Replace("<b>", "");
                temp = temp.Replace("</b>", "");
                temp = temp.Replace("<em>", "");
                temp = temp.Replace("</em>", "");
                temp = temp.Replace("\t", "");
                temp = temp.Replace("<br>", Environment.NewLine);
                temp = temp.Replace("Ã¤", "ä");
                temp = temp.Replace("&nbsp;", " ");
                temp = temp.Replace("&quot;", "\"");
                temp = temp.Replace("&#160;", "");
                temp = temp.Replace("&#8230;", "");
                temp = temp.Replace("<ul>", Environment.NewLine);
                temp = temp.Replace("</ul>", Environment.NewLine);
                temp = temp.Replace("<li>", Environment.NewLine);
                temp = temp.Replace("</li>", Environment.NewLine);
                temp = temp.Replace("<br />", Environment.NewLine);
                temp = temp.Replace("<span>", " ");
                temp = temp.Replace("</span>", " ");
                temp = temp.Replace("<div>", " ");
                temp = temp.Replace("</div>", " ");
                temp = temp.Replace("&amp;", "&");
                temp = temp.Replace("&#43;", "");

                while (temp.Contains("<a"))
                {
                    string link = temp.Substring(temp.IndexOf("<a"));
                    link = link.Substring(0, link.IndexOf(">") + 1);

                    temp = temp.Replace(link, "");
                }

                while (temp.Contains("<img"))
                {
                    string link = temp.Substring(temp.IndexOf("<img"));
                    link = link.Substring(0, link.IndexOf(">") + 1);

                    temp = temp.Replace(link, "");
                }

                while (temp.Contains("<span"))
                {
                    string link = temp.Substring(temp.IndexOf("<span"));
                    link = link.Substring(0, link.IndexOf(">") + 1);

                    temp = temp.Replace(link, "");
                }

                while (temp.Contains("<div"))
                {
                    string link = temp.Substring(temp.IndexOf("<div"));
                    link = link.Substring(0, link.IndexOf(">") + 1);

                    temp = temp.Replace(link, "");
                }
            }
            catch { }

            return temp;
        }
    }
}
